﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Newtonsoft.Json;
using System.IO;
using NesGfx;

namespace cnetmegamanprj
{
    public class LevelPtr_
    {
        public int CHR0;
        public int CHR1;
        public int palSet;

        public int levelStartScr;
        public int levelEndScr;

        public int ScrLayout;

        public int tsaTile;
        public int TSA_type;
        public int tsaPal;
        public int Structure;
        public int screen;

    }

    public abstract class MegaMan
    {
        public MegaMan(Rom rom_) { rom = rom_; }
        public Rom rom;
        protected MM_Json json;

        public byte this[int address] { get { return rom[address]; } set { rom[address] = value; } }

        //From MM_
        //public int gameNumber;
        public int level;
        public int[] ChrMapBG = new int[0x100];  //Tile index mapping for each BG VRAM tile
        public int[] ChrMapSpr = new int[0x100]; //Tile index mapping for each Spr VRAM tile
        public abstract int GameNumber { get; }

        protected LevelPtr_ levelPtr = new LevelPtr_();

        public virtual string[] LevelNames { get { return json.levelNames; } }


        //public interface IMegaMan
        //{
        public abstract int levelCount { get; }
        public abstract bool testFunc();
        public abstract void drawTiles(int level, Render render);
        public abstract void loadTsa(int level, TSAEditData tsa);
        public abstract TileEditData getTileEditData();
        public abstract LevelData getLevelData(int level);
        public abstract void SaveLevelData(LevelData lvl);
        public abstract void writeMapData(byte[,] map, byte[,] palmap, byte[,] scr_property_arr);
        //}

        public TSAEditData loadTsa(int levelNumber)
        {
            var d = new TSAEditData();
            loadTsa(levelNumber, d);
            return d;
        }


        protected void LoadJson(int gameNo)
        {
            var jsonFn = string.Format("mm{0}.json", gameNo);
            /*
            var o = new MM_Json()
            {
                levelBank = new MM_Json.LevelBank(),
                graphicsBank = new MM_Json.GraphicsBank(),
                levelByteArray = new MM_Json.LevelByteArray()
            };
            MyJSON.SaveJson(jsonFn, o);
            //*/
            json = MyJSON.LoadJson<MM_Json>(jsonFn);
        }
    }

    static class MegaManLoader
    {

        public static MegaMan load(string fn)
        {
            if (File.Exists(fn) == false)
                return null;

            int megamanNo = 1;
            if (fn.IndexOf('2') != -1)
                megamanNo = 2;
            else if (fn.IndexOf('3') != -1)
                megamanNo = 3;
            else if (fn.IndexOf('4') != -1)
                megamanNo = 4;
            else if (fn.IndexOf('5') != -1)
                megamanNo = 5;
            else if (fn.IndexOf('6') != -1)
                megamanNo = 6;

            return load(fn, megamanNo);
        }

        public static MegaMan load(string szRom, int gameNum)
        {
            Rom rom = new Rom(szRom);
            switch (gameNum)
            {
                case 2: return new MegaMan2(rom);
                default: throw new Exception("Unknown or not implemented megaman game");
            }
        }
    }


    static class MyJSON
    {

        static public T LoadJson<T>(string fn) where T : new()
        {
            string jsonsz;
            using (var f = File.OpenText(fn))
            {
                jsonsz = f.ReadToEnd();
                //f.Close();
            }

            var ser = new System.Web.Script.Serialization.JavaScriptSerializer();
            //var t = ser.Deserialize<T>(jsonsz);

            return JsonConvert.DeserializeObject<T>(jsonsz);

        }
        static public void SaveJson(string configFn, Object jsonObj)
        {
            string configSz;
            configSz = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
            using (var f = (TextWriter)File.CreateText(configFn))
            {
                f.WriteLine(configSz);
                //f.Close();
            }
        }
    }
}
